package com.lujieni.sentinel.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.lujieni.sentinel.handler.BlockHandler;
import com.lujieni.sentinel.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @Value("${name:lujieni}")
    private String name;

    /**
     * blockHandler:限流该走的方法
     * @param content
     * @return
     */
    @GetMapping(value = "/hello123")
    @SentinelResource(value = "hello123",  blockHandlerClass = BlockHandler.class, blockHandler = "blockHandlerA")
    public String hello123(String content){
        System.out.println(name);
        return "hello123";
    }

    // blockHandler对于程序中抛的业务异常无效,但对被sentinel限流的接口有效
    @GetMapping(value = "/bye")
    @SentinelResource(value = "bye", blockHandlerClass = BlockHandler.class, blockHandler = "blockHandlerA")
    public String bye(String content){
        return "success";
    }


    /**
     * fallback: 降级该走的方法
     * @param num
     * @return
     */
    @GetMapping(value = "/degrade")
    public Integer degrade(Integer num){
        return helloService.degrade(num);
    }

}
